package org.example.actualcombat.demos.web.modules.pms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.example.actualcombat.demos.web.modules.pms.model.dto.PmsProductSaveParamDTO;
import org.example.actualcombat.demos.web.modules.pms.mapper.PmsProductMapper;
import org.example.actualcombat.demos.web.modules.pms.model.*;
import org.example.actualcombat.demos.web.modules.pms.model.dto.PmsProductUpdateInitDTO;
import org.example.actualcombat.demos.web.modules.pms.model.dto.ProductConditionDTO;
import org.example.actualcombat.demos.web.modules.pms.service.*;
import org.example.actualcombat.demos.web.modules.ums.service.UmsMemberLevelService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.List;

@Service
public class PmsProductServiceImpl extends ServiceImpl<PmsProductMapper, PmsProduct> implements PmsProductService {

    @Autowired
    PmsProductMapper pmsProductMapper;
    @Autowired
    PmsMemberPriceService pmsMemberPriceService;
    @Autowired
    UmsMemberLevelService umsMemberLevelService;
    @Autowired
    PmsProductLadderService pmsProductLadderService;
    @Autowired
    PmsSkuStockService pmsSkuStockService;
    @Autowired
    PmsProductFullReductionService pmsProductFullReductionService;
    @Autowired
    PmsProductAttributeValueService pmsProductAttributeValueService;

    @Override
    public Page getList(ProductConditionDTO productConditionDTO) {
        int pageNum = productConditionDTO.getPageNum();
        int pageSize = productConditionDTO.getPageSize();
        Page<PmsProduct> page = new Page<>(pageNum,pageSize);
        QueryWrapper<PmsProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(productConditionDTO.getKeyword() != null,PmsProduct::getName,productConditionDTO.getKeyword())
                .eq(productConditionDTO.getPublishStatus() != null,PmsProduct::getPublishStatus,productConditionDTO.getPublishStatus())
                .eq(productConditionDTO.getVerifyStatus() != null,PmsProduct::getVerifyStatus,productConditionDTO.getVerifyStatus())
                .eq(productConditionDTO.getProductSn() != null,PmsProduct::getProductSn,productConditionDTO.getProductSn())
                .eq(productConditionDTO.getProductCategoryId() != null,PmsProduct::getProductCategoryId,productConditionDTO.getProductCategoryId())
                .eq(productConditionDTO.getBrandId() != null,PmsProduct::getBrandId,productConditionDTO.getBrandId())
                .eq(PmsProduct::getDeleteStatus,0)
                .orderByAsc(PmsProduct::getSort);

        return this.page(page,queryWrapper);
    }

    @Override
    public Boolean updateStatus(List<Long> ids) {
        boolean b = this.removeBatchByIds(ids);
        return b;
    }

    @Override
    public Boolean updateStatus1(Integer newStatus, List<Long> ids, SFunction<PmsProduct,?> getPublishStatus) {
        UpdateWrapper<PmsProduct> updateWrapper = new UpdateWrapper<>();
        LambdaUpdateWrapper<PmsProduct> updateWrapper1 = updateWrapper.lambda().set(getPublishStatus,newStatus).in(PmsProduct::getId, ids);

        return this.update(updateWrapper1);
    }

    @Override
    @Transactional
    public Boolean createProduct(PmsProductSaveParamDTO productSaveParamDTO)  {
//        PmsProduct product= new PmsProduct();
//        product.setId(null);
//        product.setAlbumPics("2");
//        product.setName("333®");
//        product.setProductSn("444®");
//        product.setPromotionType(4);
//        boolean result = this.save(product);
        PmsProduct product = productSaveParamDTO;
        System.out.println("product = " + product);
        System.out.println("productSaveParamDTO = " + productSaveParamDTO);
        System.out.println(productSaveParamDTO.getName());
        boolean result = this.save(product);
        if(result){
            switch (product.getPromotionType()){
                case 2:
                    System.out.println(product.getPromotionType());
                    updateStatus2(productSaveParamDTO.getMemberPriceList(),product.getId(),pmsMemberPriceService);
                    break;
                case 3:
                    System.out.println(product.getPromotionType());

                    updateStatus2(productSaveParamDTO.getProductLadderList(),product.getId(),pmsProductLadderService);

                    break;
                case 4:
                    System.out.println(product.getPromotionType());

                    updateStatus2(productSaveParamDTO.getProductFullReductionList(),product.getId(),pmsProductFullReductionService);
                    break;
            }
            updateStatus2(productSaveParamDTO.getProductAttributeValueList(),product.getId(),pmsProductAttributeValueService);
            updateStatus2(productSaveParamDTO.getSkuStockList(),product.getId(),pmsSkuStockService);

        }
        return result;

    }

    @Override
    public PmsProductUpdateInitDTO getInfo(Long id) {
        PmsProductUpdateInitDTO dto =     pmsProductMapper.getInfo(id);
//        PmsProduct byId = this.getById(id);
//        List<PmsMemberPrice> MemberPriceList = pmsMemberPriceService.lambdaQuery().eq(PmsMemberPrice::getProductId, id).list();
//        List<PmsProductLadder> pmsProductLadderList = pmsProductLadderService.lambdaQuery().eq(PmsProductLadder::getProductId, id).list();
//        List<PmsSkuStock> pmsSkuStockList = pmsSkuStockService.lambdaQuery().eq(PmsSkuStock::getProductId, id).list();
//        List<PmsProductAttributeValue> attributeValueList = pmsProductAttributeValueService.lambdaQuery().eq(PmsProductAttributeValue::getProductId, id).list();
//        List<PmsProductFullReduction> pmsProductFullReductionList = pmsProductFullReductionService.lambdaQuery().eq(PmsProductFullReduction::getProductId, id).list();
//        PmsProductUpdateInitDTO dto = new PmsProductUpdateInitDTO();
//        BeanUtils.copyProperties(byId,dto);
//        dto.setMemberPriceList(MemberPriceList);
//        dto.setProductLadderList(pmsProductLadderList);
//        dto.setSkuStockList(pmsSkuStockList);
//        dto.setProductAttributeValueList(attributeValueList);
//        dto.setProductFullReductionList(pmsProductFullReductionList);
        return dto;

    }


    public void updateStatus2(List list, Long productId, IService service) {
        if(CollectionUtil.isEmpty(list)) return;
        try{
            for (int i = 0; i < list.size(); i++) {
                Object obj = list.get(i);
                Method method = obj.getClass().getMethod("setProductId", Long.class);
                Method method1 = obj.getClass().getMethod("setId", Long.class);

                method1.invoke(obj,(Long)null);
                method.invoke(obj,productId);
            }
            boolean b = service.saveBatch(list);

        }catch (Exception err){
           throw new  RuntimeException(err);
        }

    }
}
























